package com.atguigu.process.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.model.wechat.Menu;
import com.atguigu.process.mapper.MenuMapper;
import com.atguigu.process.service.MenuService;
import com.atguigu.vo.wechat.MenuVo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private WxMpService wxMpService;

    /**
     * 查询所有微信菜单
     * @return
     */
    @Override
    public List<MenuVo> findMenuInfo() {
        List<Menu> menus = list();
        Map<Long,MenuVo> map = new HashMap<>();
        List<MenuVo> result = menus.stream().filter(m -> m.getParentId() == 0)
                .map(m -> {
                    MenuVo vo = new MenuVo();
                    BeanUtils.copyProperties(m, vo);
                    map.put(vo.getId(),vo);
                    return vo;
                }).collect(Collectors.toList());
        menus.stream().filter(m -> m.getParentId() != 0)
                .forEach(m -> {
                    MenuVo vo = new MenuVo();
                    BeanUtils.copyProperties(m, vo);
                    MenuVo parent = map.get(m.getParentId());
                    if (parent != null){
                        if(parent.getChildren() == null){
                            parent.setChildren(new ArrayList<>());
                        }
                        parent.getChildren().add(vo);
                    }
                });
        return result;
    }

    /**
     * 同步菜单
     */
    @Override
    public void syncMenu() {
        List<MenuVo> menuVoList = this.findMenuInfo();
        //菜单
        JSONArray buttonList = new JSONArray();
        for(MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            if(CollectionUtils.isEmpty(oneMenuVo.getChildren())) {
                one.put("type", oneMenuVo.getType());
                one.put("url", "http://z3a4569a.natappfree.cc/#"+oneMenuVo.getUrl());
            } else {
                JSONArray subButton = new JSONArray();
                for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                    JSONObject view = new JSONObject();
                    view.put("type", twoMenuVo.getType());
                    if(twoMenuVo.getType().equals("view")) {
                        view.put("name", twoMenuVo.getName());
                        //H5页面地址
                        view.put("url", "http://z3a4569a.natappfree.cc#"+twoMenuVo.getUrl());
                    } else {
                        view.put("name", twoMenuVo.getName());
                        view.put("key", twoMenuVo.getMeunKey());
                    }
                    subButton.add(view);
                }
                one.put("sub_button", subButton);
            }
            buttonList.add(one);
        }
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        //调用工具包方法实现菜单推送
        try {
            wxMpService.getMenuService().menuCreate(button.toString());
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除同步菜单
     */
    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
}
